package com.jrfid.smartcabinet.demo.view

import android.content.Context
import android.graphics.Color
import android.os.Parcelable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import com.blankj.utilcode.util.SizeUtils
import com.jrfid.smartcabinet.demo.R

class MRadioGroup : LinearLayout {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initAttr(context, attrs)
        initView()
    }

    private var items: MutableList<String>? = null
    private var checked: Int = 0
    private var itemWidth: Int = SizeUtils.dp2px(230f)
    private var itemHeight: Int = SizeUtils.dp2px(60f)
    private var itemSpace: Int = SizeUtils.dp2px(4f)
    private var itemTextSize: Int = SizeUtils.sp2px(18f)
    private var checkedBg: Int = R.drawable.shape_cheked_radio_btn_bg
    private var uncheckedBg: Int = R.drawable.shape_uncheked_radio_btn_bg

    @ColorInt
    private var checkedTextColor: Int = Color.WHITE

    @ColorInt
    private var uncheckedTextColor: Int = Color.parseColor("#92959C")

    private fun initAttr(context: Context, attrs: AttributeSet?) {
        val a = context.obtainStyledAttributes(attrs, R.styleable.MRadioGroup)
        val text = a.getString(R.styleable.MRadioGroup_itemsText)
        val itemsId = a.getResourceId(R.styleable.MRadioGroup_items, 0)
        if (text != null && text.isNotEmpty()) {
            items = text.split(Regex(",")).toMutableList()
        } else if (itemsId != 0) {
            items = context.resources.getStringArray(itemsId).toMutableList()
        }
        checked = a.getInt(R.styleable.MRadioGroup_checked, 0)
        itemWidth = a.getDimensionPixelSize(R.styleable.MRadioGroup_itemWidth, SizeUtils.dp2px(230f))
        itemHeight = a.getDimensionPixelSize(R.styleable.MRadioGroup_itemHeight, SizeUtils.dp2px(60f))
        itemSpace = a.getDimensionPixelSize(R.styleable.MRadioGroup_itemSpace, SizeUtils.dp2px(4f))
        itemTextSize = a.getDimensionPixelSize(R.styleable.MRadioGroup_itemTextSize, SizeUtils.sp2px(18f))
        checkedBg = a.getResourceId(R.styleable.MRadioGroup_checkedItemBackground, R.drawable.shape_cheked_radio_btn_bg)
        uncheckedBg = a.getResourceId(R.styleable.MRadioGroup_uncheckedItemBackground, R.drawable.shape_uncheked_radio_btn_bg)
        checkedTextColor = a.getColor(R.styleable.MRadioGroup_checkedTextColor, Color.WHITE)
        uncheckedTextColor = a.getColor(R.styleable.MRadioGroup_uncheckedTextColor, Color.parseColor("#92959C"))
        a.recycle()
        if (items != null)
            addGroupItems(items!!)
        setChecked(checked)
    }

    private fun initView() {

    }

    private fun addGroupItems(list: MutableList<String>) {
        removeAllViews()
        list.forEachIndexed { index, s ->
            val radioButton = TextView(context)
                .apply {
                    this.tag = index
                    this.width = itemWidth
                    this.height = itemHeight
                    this.setBackgroundResource(uncheckedBg)
                    this.gravity = Gravity.CENTER
                    this.text = s
                    this.setTextSize(TypedValue.COMPLEX_UNIT_PX, itemTextSize.toFloat())
                    this.setTextColor(uncheckedTextColor)
                    this.setOnClickListener {
                        if (checked != index) {
                            setChecked(index)
                            listener?.onCheckedChange(index)
                        }
                    }
                }
            val layoutParams = LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            layoutParams.leftMargin = if (index != 0) itemSpace else 0
            addView(radioButton, layoutParams)
        }
    }

    fun getChecked(): Int {
        return checked
    }

    fun setChecked(position: Int) {
        if (checked != -1) {
            //之前有选择的先清空
            val textView = findViewWithTag<TextView>(checked)
            textView?.setTextColor(uncheckedTextColor)
            textView?.setBackgroundResource(uncheckedBg)
        }
        if (position != -1) {
            val textView = findViewWithTag<TextView>(position)
            textView?.setTextColor(checkedTextColor)
            textView?.setBackgroundResource(checkedBg)
        }
        checked = position
    }

    private var listener: OnCheckedChangeListener? = null

    fun setOnCheckedChangeListener(listener: OnCheckedChangeListener?) {
        this.listener = listener
    }

    interface OnCheckedChangeListener {
        fun onCheckedChange(index: Int)
    }

    override fun onSaveInstanceState(): Parcelable? {
        return super.onSaveInstanceState()
    }

    override fun onRestoreInstanceState(state: Parcelable?) {
        super.onRestoreInstanceState(state)
    }

}